<template>
	<view class="content">
		<view class="nav bg-white" style="position: fixed;top: 0;width: 100%;z-index: 100;">
			<view v-for="(item, index) in category" @click="SwitchVariety(item, index)" :key="index"
				:class="{ active: activeIndex === index }" style="cursor: pointer">{{ item }}</view>
		</view>
		<view class="cont mt-10" v-if="activeIndex == 0">
			<view class="itemI">
				<view class="uni-padding-wrap uni-common-mt">
					<uni-segmented-control :current="current" :values="items" @clickItem="onClickItem"
						styleType="button" activeColor="#4cd964"></uni-segmented-control>
				</view>
				<view class="contentI">
					<view v-for="(row, i) in ApplyFinancing" :key="i">
						<view class="item">
							<view class="title">融资编号:{{ row.Order_Financing_SerialNumber }}</view>
							<view>商品价格:￥{{ row.Unit_Price }}</view>
							<view>申请数量:{{ row.Order_Financing_Quantity }}</view>
							<view>付款时间:{{ row.Repayment_Date }}天</view>
							<view>申请时间:{{ row.Application_Date }}</view>
							<view>
								<text v-if="row.Row_Status == 0">审核中</text>
								<text v-else-if="row.Row_Status == 1">已通过</text>
								<text v-else>未通过</text>
							</view>
						</view>
					</view>
				</view>
			</view>
		</view>
		<view class="cont mt-10" v-if="activeIndex == 1">
			<view class="itemI">
				<view class="contentI">
					<view v-for="(row, i) in ContractDatas" :key="i">
						<view class="item">
							<view class="title">公司名称:{{ row.Enterprise_Name }}</view>
							<view>商品名称:{{ row.Group_Purchasing_Name }}</view>
							<view>利率:{{ row.Interest_Rate }}</view>
							<view>申请人:{{ row.Order_Financing_Name }}天</view>
							<view>申请人电话:{{ row.Order_Financing_Phone }}</view>
							<view>申请数量:{{ row.Order_Financing_Quantity }}</view>
							<view>还款日期:{{ row.Repayment_Date }}天</view>
							<view>单价:{{ row.Unit_Price }}</view>
							<view>单位数量:{{ row.Unit_Quantity }}</view>
							<view>状态:
								<text v-if="row.Row_Status==0">待确认</text>
								<text v-else-if="row.Row_Status==1">待平台买货</text>
								<text v-else-if="row.Row_Status==2">待平台买货</text>
								<view v-if="row.Row_Status==3" class="flex justify-center">
									生效中{{comParetime(row.End_Date)}}
									<view class="bg-danger font-small text-white">
										{{lateTime}}
									</view>
								</view>
							</view>
						</view>
					</view>
				</view>
			</view>
		</view>
		<view class="text-center font-sm py-2">{{ loadingText }}</view>
		<view v-if="tag" @click="goTop" class="bg-white flex  align-center"
			style="width: 120rpx;height: 120rpx;border-radius: 50%;box-shadow: 0 0 15rpx rgba(0,0,0,.5);position: fixed;right: 80rpx;bottom: 80rpx;justify-content: center;">
			<image style="height: 80%;width: 80%;" src="https://digital.essc.top/dist/zjl_icon/gotop.png" mode="">
			</image>
		</view>
	</view>
</template>

<script>
	import times from '@/static/js/utils.js'
	import {
		getOrderFinancingAll,
		getFinancingContractAll,
		getRefuseIDAction,
	} from "@/api/group.js";
	import {
		getEditAction,
		getInquiryIDOriginal,
		getInquiryIDAction,
	} from "@/api/global.js";
	export default {
		data() {
			return {
				activeIndex: 0,
				items: ["全部", "审核中", "已通过", "未通过"],
				current: 0,
				category: [
					//用户专区分类筛选
					"申请合同",
					"合同管理",
				],
				lateTime: "",
				dialogTableVisible: false,
				form: {
					type: "",
					page: 1,
					limit: 4,
					token: uni.getStorageSync("token"),
				},
				form1: {
					type: "",
					page: 1,
					limit: 4,
					token: uni.getStorageSync("token"),
				},
				loading: false,
				total: 0,
				total1: 0,
				ApplyFinancing: [],
				ContractDatas: [],
				activeName: "first",
				activeType: "first",
				Reason: {},
				Concat: [],
				timer: null,
				loadingText: '向下拉取加载更多',
				scrollTop: 0,
				tag: false,
			};
		},
		onPageScroll(res) {
			this.scrollTop = res.scrollTop
		},
		watch: {
			scrollTop: function(newValue, oldValue) {
				if (newValue > 800) {
					this.tag = true
				} else {
					this.tag = false
				}
			},
		},
		onReachBottom() {
			if (this.loadingText != '没有更多了') {
				if (this.activeIndex == 0) {
					this.form.page++
					this.throttling();
				} else {
					this.form1.page++
					this.throttling1();
				}
			}
		},
		methods: {
			comParetime(time) {
				this.lateTime = times.compareTime(time)
			},
			throttling(delay = 500) {
				clearTimeout(this.timer)
				this.timer = setTimeout(() => {
					this.getList(this.form)
				}, delay)
			},
			throttling1(delay = 500) {
				clearTimeout(this.timer)
				this.timer = setTimeout(() => {
					this.getList1(this.form1)
				}, delay)
			},
			goTop() {
				uni.pageScrollTo({
					scrollTop: 0
				})
			},
			async getList(data) {
				uni.showLoading({
					title: '加载中...'
				})
				this.loadingText = '加载中...'
				const res = await getOrderFinancingAll({
					data,
				});
				uni.hideLoading()
				if (
					(res.redirect && res.redirect == "REDIRECT") ||
					res.REDIRECT == "REDIRECT"
				) {
					uni.navigateTo({
						url: "/pages/My/Login",
					});
				} else if (res.Status == "success") {
					this.ApplyFinancing.push(...res.tableData)
					console.log(res);
					this.total = Number(res.limit);
					if (this.total == this.ApplyFinancing.length) {
						this.loadingText = '没有更多了'
					} else {
						this.loadingText = '下拉加载更多'
					}
				}
			},
			async getList1(data) {
				uni.showLoading({
					title: '加载中...'
				})
				this.loadingText = '加载中...'
				const res = await getFinancingContractAll({
					data,
				});
				uni.hideLoading()
				if (
					(res.redirect && res.redirect == "REDIRECT") ||
					res.REDIRECT == "REDIRECT"
				) {
					uni.navigateTo({
						url: "/pages/My/Login",
					});
				} else if (res.Status == "success") {
					this.ContractDatas.push(...res.tableData)
					this.total1 = Number(res.limit);
					if (this.total1 == this.ContractDatas.length) {
						this.loadingText = '没有更多了'
					} else {
						this.loadingText = '下拉加载更多'
					}
				}
			},

			onClickItem(e) {
				if (this.current == e.currentIndex) {
					return
				}
				this.current = e.currentIndex;
				this.ApplyFinancing = []
				this.goTop()
				this.form.page = 1
				if (e.currentIndex == 0) {
					this.form.type = "";
					this.getList(this.form);
				} else {
					this.form.type = e.currentIndex - 1;
					this.getList(this.form);
				}
			},
			async SwitchVariety(i, index) {
				if (index == 1) {
					this.ContractDatas = []
					this.form1.page = 1;
					this.getList1(this.form1);
				} else {
					this.ApplyFinancing = []
					this.form.page = 1;
					this.getList(this.form);
				}
				this.activeIndex = index;
			},
		},
		created() {
			this.getList(this.form);
		},
	};
</script>

<style lang="scss">
	.content {
		background-color: #f5f5f5;
		// height: calc(100vh);
		width: 100%;

		.nav {
			height: 88rpx;
			background-color: #fff;
			display: flex;
			align-items: center;
			justify-content: space-around;

			// font-weight: bold;
			view {
				text-align: center;
				height: 100%;
				display: flex;
				align-items: center;
				font-size: 30rpx;
				padding: 10rpx;
				box-sizing: border-box;
			}

			.active {
				border-bottom: 3px solid #28a745;
				font-size: 30rpx;
			}
		}

		.cont {
			width: 100%;
			padding: 20rpx;
			box-sizing: border-box;
			display: flex;
			flex-direction: column;
			align-items: center;

			.uni-padding-wrap {
				background-color: #fff;
			}

			.itemI {
				width: 100%;
				border-radius: 20rpx;
				padding: 20rpx;
				box-sizing: border-box;
				margin: 0 0 30rpx 0;
			}

			.item {
				width: 100%;
				border-radius: 20rpx;
				background-color: #fff;
				padding: 20rpx;
				box-sizing: border-box;
				margin: 20rpx 0 20rpx 0;

				.title {
					font-size: 40rpx;
				}

				view {
					font-size: 28rpx;
					margin: 10rpx 0;
				}
			}
		}
	}
</style>